package com.dengpp.pdform.holder;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import com.dengpp.pdform.config.AttributeConfig;


public class TextViewHideHolder extends BaseAttributeViewHolder {
    private EditText editText;
    private TextView tvValue;
    private LinearLayout edit_text_view;
    private TextView tvTitle;
    private AttributeConfig currentAttribute;

    public TextViewHideHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tv_title);
        edit_text_view = itemView.findViewById(R.id.edit_text_view);
        editText = itemView.findViewById(R.id.edit_text);
        tvValue = itemView.findViewById(R.id.tv_value);
    }

    @Override
    public void bind(AttributeConfig attribute, boolean isEditMode, AttributeAdapter.OnAttributeChangeListener listener) {
        // 保存当前绑定的属性
        this.currentAttribute = attribute;

        if(attribute.isHide()){
            tvTitle.setText(attribute.getTitle());
            editText.setText(attribute.getValue() != null ? attribute.getValue().toString() : "");
            tvValue.setText(attribute.getValue() != null ? attribute.getValue().toString() : "");
            edit_text_view.setVisibility(View.GONE);
            // 设置最小高度为0，减少布局空间占用
            ViewGroup.LayoutParams params = itemView.getLayoutParams();
            params.height = 0;
            itemView.setLayoutParams(params);
        }
    }

    /**
     * 在 ViewHolder 被回收时清理资源
     */
    @Override
    public void onViewRecycled() {
        super.onViewRecycled();
        currentAttribute = null;
    }

}
